﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public class JLTVKSMPData : BaseFunction
    {
        VVAART VA;
        public bool checkVK07 = true;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        private Convert2Intex Convert2Intex;
        public DataSet SetCAD;
        public JLTVKSMPData()
            : base()
        {
            Helper.GetDate(ref date, ref year, ref month, ref day);
            Convert2Intex = new Convert2Intex();
        } 
        public DataSet BuildData(string client, string KRS, string LFD)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                SResult = INTEXBL.QueryVKK(MANDANT, KRS, LFD);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
        public string Today
        {
            get
            {
                DateTime now = System.DateTime.Now;
                string year = now.Year.ToString();
                string m =Right("00" +now.Month.ToString(),2);
                string day = Right("00" +now.Day.ToString(),2);
                return year + m + day;
            }
        } 
         
        public DataSet LoadQPPLAN()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwTemp;
            try
            {
                dwTemp = INTEXDB.QPPLAN.NewRow();
                dwTemp["QPPMANDANT"] = "00";
                SResult = INTEXDBC.QueryQPPLAN(dwTemp, 2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;

        }
        public DataSet Convert2QPZUORD(DataRow dwData)
        {

            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "QPZUORD_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetCAD.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "QPZUORD";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "QPZUORD"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2WAPROF(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "WAPROF_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetCAD.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "WAPROF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "WAPROF"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }

        public DataSet GetQPStep(string art, DataRow dwInput = null)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwTemp;
            DataRow dwQPZUORD;
            try
            {
                if (dwInput == null)
                {
                    dwTemp = INTEXDB.QPZUORD.NewRow();
                    dwTemp["QPZART"] = Left(art, 20);
                    dwTemp["QPZVAR"] = "   ";
                    SResult = INTEXDBC.QueryQPZUORD(dwTemp);
                    SReturn.Merge(SResult);
                    if (!SResult.Tables.Contains("QPZUORD"))
                    {
                        return SReturn;
                    }
                    if (SResult.Tables[0].Rows.Count == 0)
                        return SReturn;
                    dwQPZUORD = SResult.Tables[0].Rows[0];
                }
                else
                {
                    dwQPZUORD = dwInput;
                }
                /*
                dwTemp = INTEXDB.QPPLAN.NewRow();
                dwTemp["QPPPNRKRS"] = dwQPZUORD["QPZPNRKRS"];
                dwTemp["QPPPLFDNR"] = dwQPZUORD["QPZPLFDNR"];
                SResult = INTEXDBC.QueryQPPLAN(dwTemp);
                SReturn.Merge(SResult);
              */
                SResult = INTEXDBC.QueryQPSTEP(dwQPZUORD, 3);
                SResult.Tables[0].TableName = "ICC_QPSTEP";
                foreach (DataRow dw in SResult.Tables[0].Rows)
                {
                    dw["WAPME"] = dw["QPMMEEH"];

                }
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
    }
}